/**
 * @author Sunny xing
 */
(function(){
    this.S = {};
    var email = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
    /**
     * 显示信息
     * @param {Object} el
     * @param {string} msg
     */
    var displayMsg = function(el, msg){
        el.parent().find(".fill-tip-right").remove();
        var msgTemplate = '<div class="fill-tip-right"><span><b class="sign-bak"></b>' + msg + '</span></div>';
        el.parent().append(msgTemplate);
    }
    var hideMsg = function(el){
        el.parent().find(".fill-tip-right").remove();
    }
    //存放验证的元素
    var _stackValidateElement = {};
    var validateElement = function(vEl){
        var that = vEl, name = that.attr("name"), errorText = that.attr("data-error"), ajaxUrl = that.attr("data-ajax"), tipText = that.attr("data-tip"), val = that.val(), trimVal = $.trim(val);
        _stackValidateElement[name] = {};
        if (val.length < 1) {
            displayMsg(that, errorText);
            _stackValidateElement[name]["required"] = false;
            return;
        }
        else 
            if (val.length > 1 && trimVal.length < 1) {
                displayMsg(that, "只输入空格可是不好的行为！");
                _stackValidateElement[name]["required"] = false;
                return;
            }
            else {
                _stackValidateElement[name]["required"] = true;
                hideMsg(that)
            }
        if (that.hasClass("minlength")) {
            if (trimVal.length < that.attr("data-minlength")) {
                displayMsg(that, "请至少输入" + that.attr("data-minlength") + "个字符！");
                _stackValidateElement[name]["minlength"] = false;
                return;
            }
            else {
                _stackValidateElement[name]["minlength"] = true;
                hideMsg(that)
            }
        }
        if (that.hasClass("maxlength")) {
            if (trimVal.length > that.attr("data-maxlength")) {
                displayMsg(that, "最多输入" + that.attr("data-maxlength") + "个字符！");
                _stackValidateElement[name]["maxlength"] = false;
                return;
            }
            else {
                _stackValidateElement[name]["maxlength"] = true;
                hideMsg(that)
            }
        }
        if (that.hasClass("rangelength")) {
            if (trimVal.length > (that.attr("data-rangelength").split(","))[1] || trimVal.length < (that.attr("data-rangelength").split(","))[0]) {
                displayMsg(that, "请输入" + (that.attr("data-rangelength").split(","))[0] + "到 " + (that.attr("data-rangelength").split(","))[1] + "个字符！");
                _stackValidateElement[name]["rangelength"] = false;
                return;
            }
            else {
                _stackValidateElement[name]["rangelength"] = true;
                hideMsg(that)
            }
        }
        if (that.hasClass("mail")) {
            if (!email.test(trimVal)) {
                displayMsg(that, "请输入正确的电子邮箱！");
                _stackValidateElement[name]["email"] = false;
                return;
            }
        }
        else {
            _stackValidateElement[name]["email"] = true;
            hideMsg(that)
        }
        if (that.hasClass("ajax")) {
            _stackValidateElement[name]["ajax"] = false;
            // _stackValidateElement[name]["ajaxState"] = false;
            var data = {};
            data[name] = trimVal;
            //未发送请求
            //已发送请求
            //if(!_stackValidateElement[name]["ajaxState"]||(_stackValidateElement[name]["ajaxState"]&&_stackValidateElement[name]["ajax"])){
            $.get(ajaxUrl, data, function(r){
                if (r == "success") {
                    _stackValidateElement[name]["ajax"] = true;
                    hideMsg(that)
                }
                else {
                    _stackValidateElement[name]["ajax"] = false;
                    displayMsg(that, r);
                }
                //_stackValidateElement[name]["ajaxState"]=true;
            });
            //}
        }
    }
    /**
     * 验证函数
     * @param {Object} f 需要验证的form
     */
    var validateElementMethod = function(f){
		if(window.time){
			 clearInterval(window.time)
		}
        f.find(".required").each(function(){
            validateElement($(this));
        });
        var _getState = function(){
            var state = true;
            $.each(_stackValidateElement, function(i, n){
                $.each(n, function(j, m){
                
                    if (!m) {
                        state = false;
                        return;
                    }
                    
                });
            });
            if (state) {
                f.submit();
            }
        }
        //特殊校验
        _stackValidateElement["equalTo"] = {};
        var equalToElement = function(el, otherEl, type){
            var val = $.trim(el.val()), otherVal = $.trim(otherEl.val());
            if (val && otherVal) {
                if (val == otherVal) {
                    _stackValidateElement["equalTo"][type] = true;
                    hideMsg(otherEl)
                }
                else {
                    _stackValidateElement["equalTo"][type] = false;
                    displayMsg(otherEl, otherEl.attr("data-equalTo"));
                }
            }
        }
        equalToElement($("#password"), $("#passwordConfirm"), "password");
        window.time = setTimeout(_getState, 100);
    }
    S.validate = validateElementMethod;
    
    $(document).ready(function(){
        $(".required").focus(function(){
            if (window.time) {
                clearTimeout(window.time);
            }
        })
        $(".required").blur(function(){
            validateElement($(this));
        })
    })
})();
